<?php

/**
* Create a Select
*/
class select extends elementInner
{
    //public $options;
    public $value;

    public function select($id=NULL, $size=1, $multiple=NULL, $options, $class=NULL, $label=NULL)
    {
        parent::elementInner('select',$id, $options, $class, $label);
        $this->addAttribute('size', $size);
        $this->addAttribute('name', $id);
        $this->addAttribute('multiple', $multiple);
        $this->options = $options;
    }

    public function setValue($value)
    {
        $this->value = $value;
    }

    public function __toStringOptionsFromArray()
    {
        if (is_array($this->options))
        {
            foreach ($this->options as $line => $info)
            {
                if ( is_array($info) )
                {
                    //threat the data to get the first and second information
                    $index = 0;
                    foreach ($info as $l => $i)
                    {
                    	$l = $l;
                        $info[$index] = $i;
                        $index++;
                    }
                    $this->inner[$line] = new option($info[0], $info[1]);
                }
                else if ( is_object($info) )
                {
                    $this->inner[$line] = $info;
                }
                else
                {
                    $this->inner[$line]  = new option($line, $info);
                }
            }
        }
        else
        {
            $this->inner = new option($this->options, $this->options);
        }
    }

    public function __toString()
    {
        $this->__toStringOptionsFromArray();
        if (is_array($this->inner))
        {
            foreach ($this->inner as $line => $info)
            {
                if (is_object($info) )
                {
                    if ($info->value == $this->value)
                    {
                        $this->inner[$line]->addAttribute('selected','selected');
                    }
                }
            }
        }
        return parent::__toString();
    }

    public function setOptions($options)
    {
        $this->options = $options;
    }

    public function getOptions()
    {
        return $this->options;
    }
}
?>